/**
 * Created by kroyo on 2017/10/20.
 */



//site-toolbar   绝对定位
var $site_toolbar = $('.site-toolbar');
calWidth();
$(window).resize(function(){
	calWidth();
});
//计算window  宽度 
function calWidth(){
	var maxwidth =  $(document).width();
	var right = null,
	    minwidth = 1440;
	if(maxwidth>minwidth){
		right = (maxwidth - minwidth)*0.5;
		$site_toolbar.css('right',right);
	}else{
		$site_toolbar.css('right','0');
	}
}








//1.返回顶部按钮事件
var scrolltop;
var $goTop = $('.toolbar-topbtn');

//搜索框变量
var $search_type = $('.site-header .search .search-type'),
	$search_text = $('.site-header .search .search-text');

var is_show_in= false;
//首页
var $home_pdemand = $('.home-pdemand'),
	$headersearch = $('.header-search');
//1.1给window  添加滚动事件
$(window).scroll(function(){

    //滚动时收起下拉列表
    closeSearchType();

    //显示返回顶部按钮
    showbtn();
	var navHeight =  $('.site-header').height();
	if(scrolltop>navHeight){
	   $('.site-header').addClass('active');
	}else{
	    $('.site-header').removeClass('active');
	}
});
$goTop.click(function(){
    $('body,html').animate({ scrollTop: 0 }, 500);
})
//显示回到顶部的按钮
function showbtn(){
    scrolltop = $(window).scrollTop();
    if(scrolltop>0){
        $goTop.css('display','block');
    }else{
        $goTop.css('display','none');
    }
}
showbtn();





//2.选项卡方法  使用div自定义的select
var $search_type = $('.site-header .search-type'),
	$type_list   = $('.site-header .type-list'),
	$type_item  = $('.site-header .type-list .item');
$search_type.click(function(e){
	selectChanged(0);
	e.stopPropagation();
});
$(document).click(function(){
	closeSearchType();
});
var off = true;
function selectChanged(index){

    //判断选项卡时弹出还是隐藏  单击弹出(off=true)  双击隐藏(off=false)
    if(off){
        $search_type.addClass('active');
        $type_list.slideDown();
        off = false;
    }else{
        $search_type.removeClass('active');
        $type_list.slideUp();
        off = true;
    }
    //弹出的窗体(即选项卡)  当选择了时，标记选择并进行显示所选项的数据更新()
    $type_item.on('click',function(){
        var id = $(this).index();

        $type_item.removeClass('active');
        $type_item.eq(id).addClass('active');

        $('.site-header .type').text($(this).text());
    })
}
//收起下拉列表
function closeSearchType(){
	$search_type.removeClass('active');
    $type_list.slideUp();
    off = true;
}






//4.已登录时,点击头像跳转到个人中心,mouseover弹出下拉框
animateList('.site-header');
function animateList(sel){
	var $y_login = $(sel+' .header-login'),
		$login_user = $(sel+' .login-user');
	$y_login.mouseover(function(){
		if($('.site-header').hasClass('ylogin')){
			var height = $('.site-header .user-item').length*40;
			$login_user.css('height',height+'px');
		}
	});
	$y_login.mouseout(function(){
		$login_user.css('height','0px');
	});
}







//5.input focus
//header  search  input
inputFocus('.site-header .search-text');
function inputFocus(sel){
	var $input = $(sel+' input');
	$input.focusin(function(){
		$(this).css('border-color','#00a0e9');
		$(this).addClass('active');
	});
	$input.focusout(function(){
		$(this).css('border-color','');
		$(this).removeClass('active');
	});
}







//6.轮播图
if($('.site-banner').length>0){
	var $swiper_slide = $('.site-banner .swiper-slide');

	var $pagination = $('.site-banner .pagination'),
		$banner_btn   = $('.site-banner .banner-btn');
	
	if($swiper_slide.length>1){
		
		//6.1初始化
		swiper('.site-banner');
		
		//6.1计算控制器 pagination 宽度
		var $pagination_switch = $('.site-banner .pagination span');
		changeWidth();	
		function changeWidth(){
			var width = 22,
				widthactive = 40;
			var dwidth = width*($pagination_switch.length - 1) + widthactive + 20;	
			$pagination.css('width',dwidth);	
		}
		
		//6.2轮播图 控制方向按钮位置
		$banner_btn.css('display','none');
		var $banner_left  = $('.site-banner .banner-left'),
			$banner_right = $('.site-banner .banner-right');
		caldistance();
		$(window).resize(function(){
			caldistance();
		});
		//计算window  宽度 
		function caldistance(){
			var maxwidth =  $(document).width();
			var right = null,
			    minwidth = 1260;
			if(maxwidth>minwidth){
				right = (maxwidth - minwidth)*0.5;
				$banner_left.css('left',right);
				$banner_right.css('right',right);
			}else{
				$banner_left.css('left','0');
				$banner_right.css('right','0');
			}
		}
	}else{
		mySwiper('.site-banner');
		$pagination.css('display','none');
		$banner_btn.css('display','none');
	}
}








//7.轮播图公共方法    初始化
function swiper(sel){
	if(matchIE9()){
		//true : IE9    ;     false  : IE9以上
		var mySwiper = new Swiper(sel, {
			autoplay: 5000,
			speed:1000,
			loop:true,
			pagination : sel+' .pagination',
			paginationClickable :true,
			simulateTouch : false,
	    	autoplayDisableOnInteraction : false,
	    	preventClicks : false,
		});
		$(sel+' .banner-left').click(function(){
			mySwiper.swipePrev();
		})
		$(sel+' .banner-right').click(function(){
			mySwiper.swipeNext(); 
		})
	}else{
		var mySwiper = new Swiper(sel,{
			autoplay: 5000,
			speed:1000,
			loop:true,
			pagination : sel+' .pagination',
			paginationClickable :true,
			simulateTouch : false,
			autoplayDisableOnInteraction : false,
			preventClicks : false,
			prevButton:sel+' .banner-left',
			nextButton:sel+' .banner-right',
		})
	}
	
	
	//控制方向按钮的出现与隐藏
	var $banner_btn = $(sel+' .banner-btn');
	$(sel).mouseover(function(){
		$banner_btn.css('display','block');
	});
	$(sel).mouseout(function(){
		$banner_btn.css('display','none');
	});
	
	//控制控制器   mouseover:显示,不自动播放;   mouserout:隐藏,自动播放   
	var $banner = $(sel),
		$pagination = $(sel+' .pagination');
	$banner.mouseover(function(){
		mySwiper.stopAutoplay();
		$pagination.css('display','block');
	});
	$banner.mouseout(function(){
		mySwiper.startAutoplay();
		$pagination.css('display','none');
	});
}
//当图片小于等于一张时，不轮播
function mySwiper(sel){
	var swiper = new Swiper(sel, {
		speed:1000,
		simulateTouch : false,
    	autoplayDisableOnInteraction : false,
    	preventClicks : false,
	});
}











//8.手机号码判断
function phoneVer(sel){
//	var pattern = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/;
	var pattern = /^1[3,4,5,6,7,8,9]\d{9}$/;
	var timer = null,
		$alertview = $('.site-error'),
		$alert_text = $('.site-error .view-text'),
		$sel = $(sel+' .phone');
	if(!pattern.test($sel.val())){
		
		$sel.css('border-color','#ee0003');
		
		$alert_text.text('请输入正确的手机号码');
		$alertview.animate({
			top:'160px',
			opacity: '1'
		},1000,function(){
			timer = setTimeout(function(){
				$alertview.css('top','-100px');
				$alertview.css('opacity','0');
			},2000)
		});
		return false;
	}
	return true;
}






//9.获取url参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); //匹配目标参数
	//返回参数值
	if(r != null) {
		return unescape(r[2]);
	} else {
		return false;
	}
}








//10.视频播放窗口
var $video_wrapper  = $('.video-wrapper');
if($video_wrapper.length>0){
	$('.list-item-video .img-wrap').click(function(){
		$video_wrapper.fadeIn();
	});
	$('.video-wrapper .video-close').click(function(e){
		$video_wrapper.css('display','none');
	});
	$('.video-player').click(function(e){
		e.stopPropagation();
	});
	$video_wrapper.click(function(e){
		$video_wrapper.css('display','none');
	});
}








//10.切换显示‘发布需求’与‘发布作品’按钮       true：发布需求  ;   false： 发布作品
function changeHeaderbtn(off){
	var $pdemand = $('.site-header .header-pdemand'),
		$pcasebtn = $('.site-header .header-pcasebtn');
	if(off){
		$pdemand.css('display','block');
		$pcasebtn.css('display','none');
	}else{
		$pdemand.css('display','none');
		$pcasebtn.css('display','block');
	}
}





//11.判断是否登录  改变视图     true：已登录  ;   false： 未登录
function loginChange(off){
	var $site_header = $('.site-header');
	if(off){
		$site_header.addClass('ylogin');
	}else{
		$site_header.removeClass('ylogin');
	}
}


var $success_view = $('.site-success'),
    $success_text = $('.site-success .view-text'),
    $error_view = $('.site-error'),
    $error_text = $('.site-error .view-text');
    

//14.信息提示框 弹出
function viewAnimated($alertview,$alert_text,text){
	$alert_text.text(text);
	$alertview.animate({
		top:'160px',
		opacity: '1'
	},1000,function(){
		var timer = setTimeout(function(){
			$alertview.css('top','-100px');
			$alertview.css('opacity','0');
		},2000);
	});
}
function alertbox(msg){
	$error_text.text(msg);
	$error_view.animate({
		top:'160px',
		opacity: '1'
	},1000,function(){
		var timer = setTimeout(function(){
			$error_view.css('top','-100px');
			$error_view.css('opacity','0');
		},2000);
	});
}
function successbox(msg){
	$success_text.text(msg);
	$success_view.animate({
		top:'160px',
		opacity: '1'
	},1000,function(){
		var timer = setTimeout(function(){
			$success_view.css('top','-100px');
			$success_view.css('opacity','0');
		},2000);
	});
}





//15.更多导航下拉
navMore();
function navMore(){
	var $nav_more = $('.site-header .nav-more'),
		$nav_more_a = $('.site-header .nav-more a'),
		$more_list = $('.site-header .more-list');
	
	$nav_more.mouseover(function(){
		$more_list.css('height','80px');
	});
	$nav_more.mouseout(function(){
		$more_list.css('height','0');
	});
}














//15.浏览器判断
function matchIE9(){
	//true : IE9  ;    false : 不是IE9
	if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/9./i)=="9."){ 
		return true;
	}else{
		return false;
	}
}
function matchIE8(){
	//true : IE9及以下版本  ;    false : IE9及以上版本
	if(navigator.appName == "Microsoft Internet Explorer"&&parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE",""))<9){
	    alert("您的浏览器版本过低，请下载IE9及以上版本");
	    return true;
	}else{
		return false;
	}
}









//16.弹窗的公共方法   弹出   fadeIn   fadeOut
function siteWindow(sel,selector){
	//1.关闭按钮
	$(sel+' .btn-close').click(function(){
		$(sel+' input[type="reset"]').trigger('click');
		$(sel+ ' input').css('border-color','');
		winFade($(sel),false);
	});
	
	//点击content 不关闭弹窗
	$(sel+' .container>div').click(function(e){
		e.stopPropagation();
	});
	//2.点击content之外的内容    关闭弹窗
	$(sel).click(function(){
		$(sel+' .btn-close').trigger('click');
	});
}
//overflow hidden     true :  hidden  ;     false :  overlay
function  winFade($sel,off){
	if(off){
		$sel.css('display','block');
		$('body').css('overflow','hidden');
	}else{
		$sel.css('display','none');
		$('body').css('overflow','overlay');
	}
}






// footer 友情链接
footerLink();
function footerLink(){
	var $downbtn = $('.site-footer .link-downbtn'),
		$linkList = $('.site-footer .link-list');
	var off = true;
	$downbtn.on('click',function(){
		var _this = $(this);
		_this.toggleClass('active');
		$linkList.fadeToggle();
		if(off){
			_this.text('收起');
			off = false;
			return false;
		}
		off = true;
		_this.text('查看全部');
	});
}
